import Vue from "vue";
import Router from "vue-router";

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);

import Layout from "@/layout";
/**
 * constantRoutes
 * 没有权限要求的页面
 * 所有角色都可以访问
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [{
      path: "dashboard",
      name: "首页",
      component: () => import("@/views/dashboard/index"),
      meta: {
        title: "首页",
        icon: "el-icon-s-home"
      }
    }]
  },
  {
    path: "/webmap",
    component: Layout,
    name: "webmap",
    meta: {
      title: "高德地图使用",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "roleRule",
        component: () => import("@/views/componentsDemo/webmap/index.vue"),
        name: "高德地图使用",
        meta: {
          title: "高德地图使用",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/toDoList",
    component: Layout,
    name: "toDoList",
    meta: {
      title: "toDoList",
      icon: "el-icon-s-help"
    },
    children: [
      {
        path: "roleRule",
        component: () => import("@/views/componentsDemo/toDoList/index.vue"),
        name: "toDoList",
        meta: {
          title: "toDoList",
          icon: "el-icon-s-help"
        }
      }
    ]
  },
  {
    path: "/selectLi",
    component: Layout,
    name: "selectLi",
    meta: {
      title: "列表选择",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "roleRule",
        component: () => import("@/views/componentsDemo/selectLi/index.vue"),
        name: "列表选择",
        meta: {
          title: "列表选择",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/boxLeftCenter",
    component: Layout,
    name: "boxLeftCenter",
    meta: {
      title: "居中+左对齐布局",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "boxLeftCenter",
        component: () => import("@/views/componentsDemo/boxLeftCenter/index.vue"),
        name: "居中+左对齐布局",
        meta: {
          title: "居中+左对齐布局",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },

  {
    path: "/webSocket",
    component: Layout,
    name: "webSocket",
    meta: {
      title: "webSocket使用",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "webSocket",
        component: () => import("@/views/componentsDemo/webSocket/index.vue"),
        name: "webSocket使用",
        meta: {
          title: "webSocket使用",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },

  // {
  //   path: "/sticky",
  //   component: Layout,
  //   name: "sticky",
  //   meta: {
  //     title: "吸顶/返回顶部组件",
  //     icon: "el-icon-s-help",
  //     roles: ["qm:set:model"]
  //   },
  //   children: [
  //     {
  //       path: "roleRule",
  //       component: () => import("@/views/componentsDemo/sticky/index.vue"),
  //       name: "吸顶/返回顶部组件使用",
  //       meta: {
  //         title: "吸顶/返回顶部使用",
  //         icon: "el-icon-s-help",
  //         roles: ["qm:set:model"]
  //       }
  //     }
  //   ]
  // },
  {
    path: "/simpleForm",
    component: Layout,
    name: "简单的表单组件",
    meta: {
      title: "简单的表单组件",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "useSimpleForm",
        component: () => import("@/views/componentsDemo/useSimpleForm/index.vue"),
        name: "使用表单组件",
        meta: {
          title: "使用表单组件",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/datagrid",
    component: Layout,
    name: "简单的表格组件",
    meta: {
      title: "简单的表格组件",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "dataGrid",
        component: () => import("@/views/componentsDemo/useDataGrid/index.vue"),
        name: "简单的表格+导出",
        meta: {
          title: "简单的表格+导出",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      },
      {
        path: "useTableData1",
        component: () => import("@/views/componentsDemo/useTableData1/index.vue"),
        name: "弹框表格+表格分页打印",
        meta: {
          title: "弹框表格+分页打印",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      },
      {
        path: "useTableData2",
        component: () => import("@/views/componentsDemo/useTableData2/index.vue"),
        name: "复杂的表格导出",
        meta: {
          title: "复杂的表格导出",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/mes",
    component: Layout,
    name: "mes",
    meta: {
      title: "mes",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "useComponent",
        component: () => import("@/views/mesComponebtsDome/index.vue"),
        name: "mes组件的使用",
        meta: {
          title: "mes组件的使用",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },

  {
    path: "/directiveDemo",
    component: Layout,
    name: "可拖拽弹框",
    meta: {
      title: "可拖拽弹框",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "useDirectiveDemo",
        component: () => import("@/views/directiveDemo/elDragDialog/index.vue"),
        name: "可拖拽弹框使用",
        meta: {
          title: "可拖拽弹框使用",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/soundCode",
    component: Layout,
    name: "源码",
    meta: {
      title: "源码",
      icon: "el-icon-s-help",
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "mustache",
        name: "mustache",
        component: () => import("@/views/soundCode/mustache/index.vue"),
        meta: {
          title: "mustache",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        },
        children: [
          {
            path: "mustacheCode",
            component: () => import("@/views/soundCode/mustache/useMustache.vue"),
            name: "mustacheCode",
            meta: {
              title: "mustacheCode",
              icon: "el-icon-s-help",
              roles: ["qm:set:model"]
            }
          },
          {
            path: "mustacheTemplate",
            component: () => import("@/views/soundCode/mustache/mustacheTemplate.vue"),
            name: "简单的模板引擎",
            meta: {
              title: "简单的模板引擎",
              icon: "el-icon-s-help",
              roles: ["qm:set:model"]
            }
          }
        ]
      },
      {
        path: "snabbdomeCode",
        component: () => import("@/views/soundCode/snabbdom/index.vue"),
        name: "snabbdome",
        meta: {
          title: "snabbdome",
          icon: "el-icon-s-help",
          roles: ["qm:set:model"]
        },
        children: [
          {
            path: "snabbdom",
            component: () => import("@/views/soundCode/snabbdom/useSnabbdom"),
            name: "使用snabbdom",
            meta: {
              title: "使用snabbdom",
              icon: "el-icon-s-help",
              roles: ["qm:set:model"]
            }
          },
          {
            path: "studySnabbdom",
            component: () => import("@/views/soundCode/snabbdom/studySnabbdom"),
            name: "手写snabbdom",
            meta: {
              title: "手写snabbdom",
              icon: "el-icon-s-help",
              roles: ["qm:set:model"]
            }
          },
          {
            path: "diff",
            component: () => import("@/views/soundCode/snabbdom/studySnabbdom/imges.vue"),
            name: "diff算法相关图片",
            meta: {
              title: "diff算法相关图片",
              icon: "el-icon-s-help",
              roles: ["qm:set:model"]
            }
          }
        ]
      }
    ]
  },
  {
    path: "/personManage",
    component: Layout,
    name: "插槽使用",
    meta: {
      title: "插槽使用",
      icon: "el-icon-s-help",
      // roles: ["empInfo", "staffAnalyse"]
      roles: ["qm:set:model"]
    },
    children: [
      {
        path: "actionScope",
        name: "具名插槽",
        component: () => import("@/views/componentsDemo/slot/actionScope/index"),
        meta: {
          title: "具名插槽",
          icon: "table",
          roles: ["qm:set:model"]
        }
      },
      {
        path: "slot",
        name: "插槽",
        component: () => import("@/views/componentsDemo/slot/slot/index"),
        meta: {
          title: "插槽",
          icon: "chartPie",
          roles: ["qm:set:model"]
        }
      }
    ]
  },
  {
    path: "/authority",
    component: Layout,
    name: "用户权限",
    redirect: "/authority/userAccount",
    meta: {
      title: "用户权限",
      icon: "jurisdiction",
      roles: ["qm:auth"]
    },
    children: [
      {
        path: "userAccount",
        component: () => import("@/views/systemRule/empInfo/index"),
        meta: { title: "用户信息", icon: "userMsg", roles: ["qm:auth:user"] }
      },
      {
        path: "dataDict",
        component: () => import("@/views/systemRule/dictionary/index"),
        meta: { title: "数据字典", icon: "dictionaries", roles: ["qm:set:dictionary"] }

      },
      {
        path: "roleRule",
        name: "角色权限配置",
        component: () => import("@/views/systemRule/roleRule/index"),
        meta: { title: "角色权限配置", icon: "roleConfiguration", roles: ["qm:auth:role"] }
      }
    ]
  }
];

/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 *  roles 每个路由对应一个角色有这个角色那么有页面的访问权限
 */
export const asyncRoutes = [

];

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
});

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
